package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.AttentionDao;
import com.gdit.zax.dao.GroupDao;
import com.gdit.zax.entitys.AttentionUser;
import com.gdit.zax.entitys.GroupAll;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.VeriFicUtil;

public class GetSchedule extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
		//检测连接、用户
		if (uid==Integer.MIN_VALUE || uid == Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		AttentionDao adao = dao.getAttentionDao();
		ArrayList<AttentionUser> list = adao.listAttention(uid, 1, 10000);
		ArrayList<AttentionUser> teachers = new ArrayList<AttentionUser>();
		ArrayList<AttentionUser> friends = new ArrayList<AttentionUser>();
		for (AttentionUser attentionUser : list) {
			if (attentionUser.getT_user_tlevel()>=Constant.ULEVEL_teacher) {
				teachers.add(attentionUser);
			} else {
				friends.add(attentionUser);
			}
		}
//		GroupDao gdao = dao.getGroupDao();
//		List<GroupAll> list = gdao.getMyGroup(uid, 1, 1000);
		Map<String, Object> out = new HashMap<String,Object>();
		out.put("Teachers", teachers);
		out.put("Friends", friends);
//		for (GroupAll groupAll : list) {
//			HashMap<String, Object> map = new HashMap<String, Object>();
//			map.put("cid", groupAll.getT_group_id());
//			map.put("pic", groupAll.getT_group_pic());
//			map.put("classname", groupAll.getT_group_name());
//			
//			map.put("calssmembersum", gdao.getGroupSubSum(groupAll.getT_group_id()));
//			UserInfo uinfo = gdao.getGroupTeacherInfo(groupAll.getT_group_id());
//			if (uinfo == null) {
//				map.put("teachername", "管理员");	
//			} else {
//				map.put("teachername", uinfo.getNick());			
//			}
//			out.add(map);
//		}
		O.printSuccess(response, out);
	}

}
